﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Helper;
using SdmxMl.Panels;


namespace SdmxMl.UC
{
    public partial class UcGroup : UserControl
    {
        protected TreeNode treeNode;
        private GroupType grp;
        private KeyFamilyType dsd;
        private PanelAnnotationList pannelAnnot;
        private bool _isFinal;

        public UcGroup()
        {
            InitializeComponent();
        }

        public virtual void BindFromNode(TreeNode node, PanelAnnotationList panelAnnotation, bool isFinal)
        {
            treeNode = node;
            _isFinal = isFinal;
            pannelAnnot = panelAnnotation;
            grp = null;
            dsd = null;
            if (node != null)
            {
                grp = node.Tag as GroupType;
                dsd = TreeHelper.GetParentArtefact(node) as KeyFamilyType;

            }
            BindToControls();

        }

        private void BindToControls()
        {
            if (grp != null)
            {
                Visible = true;

                textBoxValue.Text = grp.Id;
                groupBoxDim.Enabled = true;
                
                // Fill lists
                ucDualList.ListAssigned.Items.Clear();
                ucDualList.ListAssigned.Sorted = false;
                ucDualList.ListAvailable.Items.Clear();
                ucDualList.ListAvailable.Sorted = false;

                // all Dimensions of DSD with role Dimension.
                List<string> ld = dsd.DimensionList.GetDimensionRoleList();
                // To order as in dimension 
                List<string> iter = dsd.DimensionList.GetDimensionRoleList();
                foreach (string s in iter)
                {
                    if (grp.DimensionRefList.Contains(s))
                    {
                        ucDualList.ListAssigned.Items.Add(s);
                        ld.Remove(s);
                    }
                }

                // Still availables
                ucDualList.ListAvailable.Items.AddRange(ld.ToArray());           

                pannelAnnot.BindFromNode(grp, _isFinal);

                // iS FINAL TREATMENT
                ucDualList.Enabled = _isFinal == false;
                textBoxValue.Enabled = _isFinal == false;
            }
            else
                Visible = false;
        }

        public bool UpdateNodeTag(TreeNode node)
        {
            bool err = false;
            if (node != null)
            {
                err = BindFromControls();
                if (grp != null && node.Text != grp.ToString())
                    node.Text = grp.ToString();
            }

            return err; // true on error
        }

        protected bool BindFromControls()
        {
            bool err = false;
            if (grp != null)
            {
                grp.Id = textBoxValue.Text;

                grp.DimensionRefList.Clear();
                foreach (string s in ucDualList.ListAssigned.Items )
                    grp.DimensionRefList.Add(s);
           }
            return err;
        }

    }
}
